package com.liu.tcpDemo.Client;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

/*监听服务端的推送*/
public class ClientListen implements Runnable{
    private Socket socket;

    public ClientListen(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
//        输入流接收服务端推送
        try {
            ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
            while (true){
                JSONObject jsonObject=(JSONObject)ois.readObject();
                System.out.println("来自服务端的消息："+jsonObject.get("chat"));
                System.out.println("请输入：");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
